package admin

import (
	ct "context"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"strings"
	. "xm_shop/database"
	. "xm_shop/models"
	pbManager "xm_shop/proto/rbacManager"
	. "xm_shop/utils"
)

type ManagerController struct{}

// 增加管理员
func (mag ManagerController) AddManager(context *gin.Context) {
	roleIdStr := context.PostForm("role_id") // 就算是前端传的是int型, 这里抓到的统一是字符串
	roleId, err := strconv.Atoi(roleIdStr)
	if err != nil {
		context.String(http.StatusOK, "类型转换出错, 参数不合法")
		return
	}
	username := strings.Trim(context.PostForm("username"), " ")
	password := strings.Trim(context.PostForm("password"), " ")
	email := strings.Trim(context.PostForm("email"), " ")
	mobile := strings.Trim(context.PostForm("mobile"), " ")
	// 判断用户名和密码长度是否合法
	if len(username) < 2 || len(password) < 6 {
		context.String(http.StatusOK, "参数不合法")
		return
	}
	// 判断管理员是否存在
	managerList := []Manager{}
	DB.Where("username = ?", username).Find(&managerList)
	if len(managerList) > 0 {
		context.String(http.StatusOK, "管理员已存在")
		return
	}
	rbacClient := pbManager.NewRbacManagerService("rbac", MicroClient)
	res, _ := rbacClient.ManagerAdd(ct.Background(), &pbManager.ManagerAddRequest{
		Username: username,
		Password: Md5(password),
		Email:    email,
		Mobile:   mobile,
		RoleId:   int64(roleId),
	})
	context.JSON(http.StatusOK, gin.H{
		"message": res.Message,
		"success": res.Success,
	})
}

// 获取管理员列表
func (mag ManagerController) GetManager(context *gin.Context) {
	rbacClient := pbManager.NewRbacManagerService("rbac", MicroClient)
	res, _ := rbacClient.ManagerGet(ct.Background(), &pbManager.ManagerGetRequest{})
	context.JSON(http.StatusOK, gin.H{
		"managerList": res.ManagerList,
	})
}

// 获取管理员信息
func (mag ManagerController) GetOnlyManager(context *gin.Context) {
	managerIdStr := context.Param("id") // 获取到的是字符串
	// 转整型
	manageId, err := strconv.Atoi(managerIdStr)
	if err != nil {
		context.String(http.StatusOK, "类型转换出错, 参数有误")
		return
	}
	rbacClient := pbManager.NewRbacManagerService("rbac", MicroClient)
	res, _ := rbacClient.ManagerGet(ct.Background(), &pbManager.ManagerGetRequest{
		Id: int64(manageId),
	})
	context.JSON(http.StatusOK, gin.H{
		"managerList": res.ManagerList[0],
	})
}

// 编辑管理员信息
func (mag ManagerController) EditManager(context *gin.Context) {
	managerIdStr := context.Param("id")
	managerId, err := strconv.Atoi(managerIdStr)
	if err != nil {
		context.String(http.StatusOK, "类型转换出错, 参数有误")
		return
	}
	RoleIdStr := context.PostForm("role_id")
	RoleId, RoleErr := strconv.Atoi(RoleIdStr)
	if RoleErr != nil {
		context.String(http.StatusOK, "类型转换出错, 参数有误")
		return
	}
	username := strings.Trim(context.PostForm("username"), " ")
	password := strings.Trim(context.PostForm("password"), " ")
	email := strings.Trim(context.PostForm("email"), " ")
	mobile := strings.Trim(context.PostForm("mobile"), " ")
	// 判断手机号长度
	if len(mobile) != 11 {
		context.String(http.StatusOK, "手机号长度不合法")
		return
	}
	password = Md5(password)
	rbacClient := pbManager.NewRbacManagerService("rbac", MicroClient)
	res, _ := rbacClient.ManagerEdit(ct.Background(), &pbManager.ManagerEditRequest{
		Id:       int64(managerId),
		Username: username,
		Password: password,
		Email:    email,
		Mobile:   mobile,
		RoleId:   int64(RoleId),
	})
	context.JSON(http.StatusOK, gin.H{
		"message": res.Message,
		"success": res.Success,
	})
}

// 删除管理员
func (mag ManagerController) DelManager(context *gin.Context) {
	managerIdStr := context.Param("id")
	// 类型转换
	managerId, err := strconv.Atoi(managerIdStr)
	if err != nil {
		context.String(http.StatusBadRequest, "参数不合法")
		return
	}
	rbacClient := pbManager.NewRbacManagerService("rbac", MicroClient)
	res, _ := rbacClient.ManagerDelete(ct.Background(), &pbManager.ManagerDeleteRequest{
		Id: int64(managerId),
	})
	context.JSON(http.StatusOK, gin.H{
		"message": res.Message,
		"success": res.Success,
	})
}
